export default class SessionStorageProxy<T extends object> {
  private namespace: string;
  private proxyInstance: T;

  constructor(namespace: string) {
    this.namespace = namespace;
    this.proxyInstance = new Proxy({} as T, {
      get: (target, prop) => {
        const stringifiedValue = sessionStorage.getItem(`${this.namespace}.${prop.toString()}`);
        if (!stringifiedValue) {
          return '';
        }
        return JSON.parse(stringifiedValue);
      },
      set: (target, prop, newValue) => {
        sessionStorage.setItem(`${this.namespace}.${prop.toString()}`, JSON.stringify(newValue));
        return true;
      },
    });
  }

  public getNamespace = () => this.namespace;

  public getInstance = () => this.proxyInstance;
}
